//
//  IHAdjustLocationButton.swift
//  InternetHospital
//
//  Created by Jekin on 2019/1/11.
//  Copyright © 2019 ZhuJiangChilink. All rights reserved.
//

import UIKit
import JKUIViewExtension
enum IHButtonEdgeInsetsStyle {
    case right
    case left
    case top
    case bottom
}

class IHAdjustLocationButton: UIButton {
 
   public var style : IHButtonEdgeInsetsStyle = .right
    public var space : CGFloat = 0
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let imageWidth = imageView?.width ?? 0.0
        let imageHeight = imageView?.height ?? 0.0
        let labelWidth = titleLabel?.width ?? 0.0
        let labelHeight = titleLabel?.height ?? 0.0
        var imageEdgeInsets = UIEdgeInsets.zero
        var labelEdgeInsets = UIEdgeInsets.zero
        switch self.style {
        case .right:
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+self.space/2.0, 0, -labelWidth-self.space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth-self.space/2.0, 0, imageWidth+self.space/2.0);
        case .top:
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight - space/2.0,0 , 0, -labelWidth)
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, -imageHeight-space/2.0, 0)
        case .left:
            imageEdgeInsets = UIEdgeInsetsMake(0, -self.space/2.0, 0, self.space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, self.space/2.0, 0, -self.space/2.0);
        case .bottom:
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-self.space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-self.space/2.0, -imageWidth, 0, 0);
 
        }
        
        titleEdgeInsets = labelEdgeInsets
        self.imageEdgeInsets = imageEdgeInsets
    }
}
